/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.upn.sigemfi.vfi.converters;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import edu.upn.sigemfi.ope.be.Moneda;
import edu.upn.sigemfi.vfi.bc.MonedaDelegate;
import edu.upn.sigemfi.vfi.beans.MonedaController;
/**
 *
 * @author Alex
 */
public class MonedaConverter implements Converter{



        @Override
    public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) throws ConverterException {
      MonedaDelegate delegado= new MonedaDelegate();
        if (arg2 == null) {
            return null;
        }
        try {
            Long codigo = Long.parseLong(arg2);
            if (codigo != null) {
                Moneda det = delegado.buscar(codigo);
                return det;
            } else {
                return null;
            }
        } catch (Exception e) {
        
            e.printStackTrace();
        }
        return null;
       
    }

  @Override
    public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) throws ConverterException {
        if (arg2 == null) {
            return null;
        }
        if (arg2 instanceof Moneda) {
            Moneda mo = (Moneda) arg2;
            return mo.getIdMoneda().toString();
        } else {
            throw new IllegalArgumentException("El objeto que se ha pasado no es de tipo moneda");
        }
    }
    }

 



